home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / NewsView 1.0.0 / source / Main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-16  |  8.9 KB  |  353 lines  |  [TEXT/KAHL]

  1. /* Main.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Offline USENET News Viewer                                             */
  5. /*    Written by Thomas R. Lawrence, 1993 - 1994.                            */
  6. /*                                                                           */
  7. /*    This software is Public Domain; it may be used for any purpose         */
  8. /*    whatsoever without restriction.                                        */
  9. /*                                                                           */
  10. /*    This package is distributed in the hope that it will be useful,        */
  11. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  12. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                   */
  13. /*                                                                           */
  14. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  15. /*                                                                           */
  16. /*****************************************************************************/
  17.  
  18. #include "MiscInfo.h"
  19. #include "Audit.h"
  20. #include "Debug.h"
  21. #include "Definitions.h"
  22.  
  23. #define CompilingMainC
  24. #include "Main.h"
  25. #include "Menus.h"
  26. #include "Screen.h"
  27. #include "Alert.h"
  28. #include "GrowIcon.h"
  29. #include "WindowDispatcher.h"
  30. #include "Displayer.h"
  31. #include "StartupOpen.h"
  32. #include "Files.h"
  33.  
  34.  
  35. MenuType*                    mmAppleMenu;
  36. MenuType*                    mmFileMenu;
  37. MenuType*                    mmEditMenu;
  38.  
  39. MenuItemType*            mAboutThisProgram;
  40. MenuItemType*            mOpenFile;
  41. MenuItemType*            mSaveArticle;
  42. MenuItemType*            mAppendArticle;
  43. MenuItemType*            mSaveSelection;
  44. MenuItemType*            mAppendSelection;
  45. MenuItemType*            mCloseFile;
  46. MenuItemType*            mQuit;
  47. MenuItemType*            mUndo;
  48. MenuItemType*            mCut;
  49. MenuItemType*            mCopy;
  50. MenuItemType*            mPaste;
  51. MenuItemType*            mClear;
  52. MenuItemType*            mSelectAll;
  53. MenuItemType*            mEnterSelection;
  54. MenuItemType*            mFind;
  55. MenuItemType*            mFindAgain;
  56. MenuItemType*            mBalanceParens;
  57. MenuItemType*            mGotoLine;
  58. MenuItemType*            mShowSelection;
  59.  
  60.  
  61. int                                main(int argc, char* argv[])
  62.     {
  63.         MyBoolean                ContinueFlag;
  64.  
  65.  
  66.         APRINT(("+main"));
  67.  
  68.         /* level 0 initialization */
  69.         if (!InitializeScreen())
  70.             {
  71.                 goto InitScreenFailedPoint;
  72.             }
  73.  
  74.         /* level 1 initialization */
  75.         if (!InitializeAlertSubsystem())
  76.             {
  77.                 goto InitAlertFailedPoint;
  78.             }
  79.         if (!InitializeGrowIcon())
  80.             {
  81.                 goto InitGrowFailedPoint;
  82.             }
  83.         if (!InitializeWindowDispatcher())
  84.             {
  85.                 goto InitWindowDispatcherFailed;
  86.             }
  87.  
  88.         /* application initialization */
  89.         if (!InitializeDisplayer())
  90.             {
  91.                 goto InitWindowStuffFailed;
  92.             }
  93.  
  94.         mmAppleMenu = MakeAppleMenu();
  95.         if (mmAppleMenu == NIL)
  96.             {
  97.                 goto MakeAppleMenuFailed;
  98.             }
  99.         mAboutThisProgram = MakeNewMenuItem(mmAppleMenu,"About NewsView...",0);
  100.         if (mAboutThisProgram == NIL)
  101.             {
  102.                 goto MakeFileMenuFailed;
  103.             }
  104.         ShowMenu(mmAppleMenu);
  105.  
  106.         mmFileMenu = MakeNewMenu("File");
  107.         if (mmFileMenu == NIL)
  108.             {
  109.                 goto MakeFileMenuFailed;
  110.             }
  111.         mOpenFile = MakeNewMenuItem(mmFileMenu,"Open...",'O');
  112.         if (mOpenFile == NIL)
  113.             {
  114.                 goto MakeEditMenuFailed;
  115.             }
  116.         mCloseFile = MakeNewMenuItem(mmFileMenu,"Close",'W');
  117.         if (mCloseFile == NIL)
  118.             {
  119.                 goto MakeEditMenuFailed;
  120.             }
  121.         AppendSeparator(mmFileMenu);
  122.         mSaveArticle = MakeNewMenuItem(mmFileMenu,"Save Article...",'S');
  123.         if (mSaveArticle == NIL)
  124.             {
  125.                 goto MakeEditMenuFailed;
  126.             }
  127.         mAppendArticle = MakeNewMenuItem(mmFileMenu,"Append Article To File...",0);
  128.         if (mAppendArticle == NIL)
  129.             {
  130.                 goto MakeEditMenuFailed;
  131.             }
  132.         mSaveSelection = MakeNewMenuItem(mmFileMenu,"Save Selection...",0);
  133.         if (mSaveSelection == NIL)
  134.             {
  135.                 goto MakeEditMenuFailed;
  136.             }
  137.         mAppendSelection = MakeNewMenuItem(mmFileMenu,"Append Selection To File...",0);
  138.         if (mAppendSelection == NIL)
  139.             {
  140.                 goto MakeEditMenuFailed;
  141.             }
  142.         AppendSeparator(mmFileMenu);
  143.         mQuit = MakeNewMenuItem(mmFileMenu,"Quit",'Q');
  144.         if (mQuit == NIL)
  145.             {
  146.                 goto MakeEditMenuFailed;
  147.             }
  148.         ShowMenu(mmFileMenu);
  149.  
  150.         mmEditMenu = MakeNewMenu("Edit");
  151.         if (mmEditMenu == NIL)
  152.             {
  153.                 goto MakeEditMenuFailed;
  154.             }
  155.         mUndo = MakeNewMenuItem(mmEditMenu,"Undo",'Z');
  156.         if (mUndo == NIL)
  157.             {
  158.                 goto AfterMenuFailed;
  159.             }
  160.         AppendSeparator(mmEditMenu);
  161.         mCut = MakeNewMenuItem(mmEditMenu,"Cut",'X');
  162.         if (mCut == NIL)
  163.             {
  164.                 goto AfterMenuFailed;
  165.             }
  166.         mCopy = MakeNewMenuItem(mmEditMenu,"Copy",'C');
  167.         if (mCopy == NIL)
  168.             {
  169.                 goto AfterMenuFailed;
  170.             }
  171.         mPaste = MakeNewMenuItem(mmEditMenu,"Paste",'V');
  172.         if (mPaste == NIL)
  173.             {
  174.                 goto AfterMenuFailed;
  175.             }
  176.         mClear = MakeNewMenuItem(mmEditMenu,"Clear",0);
  177.         if (mClear == NIL)
  178.             {
  179.                 goto AfterMenuFailed;
  180.             }
  181.         mSelectAll = MakeNewMenuItem(mmEditMenu,"Select All",'A');
  182.         if (mSelectAll == NIL)
  183.             {
  184.                 goto AfterMenuFailed;
  185.             }
  186.         AppendSeparator(mmEditMenu);
  187.         mEnterSelection = MakeNewMenuItem(mmEditMenu,"Enter Selection",'E');
  188.         if (mEnterSelection == NIL)
  189.             {
  190.                 goto AfterMenuFailed;
  191.             }
  192.         mFind = MakeNewMenuItem(mmEditMenu,"Find...",'F');
  193.         if (mFind == NIL)
  194.             {
  195.                 goto AfterMenuFailed;
  196.             }
  197.         mFindAgain = MakeNewMenuItem(mmEditMenu,"Find Again",'G');
  198.         if (mFindAgain == NIL)
  199.             {
  200.                 goto AfterMenuFailed;
  201.             }
  202.         AppendSeparator(mmEditMenu);
  203.         mBalanceParens = MakeNewMenuItem(mmEditMenu,"Balance Parentheses",'B');
  204.         if (mBalanceParens == NIL)
  205.             {
  206.                 goto AfterMenuFailed;
  207.             }
  208.         mGotoLine = MakeNewMenuItem(mmEditMenu,"Goto Line...",'J');
  209.         if (mGotoLine == NIL)
  210.             {
  211.                 goto AfterMenuFailed;
  212.             }
  213.         mShowSelection = MakeNewMenuItem(mmEditMenu,"Show Selection",0);
  214.         if (mShowSelection == NIL)
  215.             {
  216.                 goto AfterMenuFailed;
  217.             }
  218.         ShowMenu(mmEditMenu);
  219.  
  220.         PrepareStartupDocuments(argc,argv);
  221.         ContinueFlag = True;
  222.         while (ContinueFlag)
  223.             {
  224.                 OrdType                    XLoc;
  225.                 OrdType                    YLoc;
  226.                 ModifierFlags        Modifiers;
  227.                 FileSpec*                StartupItem;
  228.                 WinType*                Window;
  229.                 MenuItemType*        MenuItem;
  230.                 char                        KeyPress;
  231.                 EventType                TheEvent;
  232.  
  233.                 /* see if there are any startup items to open */
  234.                 if (GetStartupObject(&StartupItem))
  235.                     {
  236.                         /* if it returns True, then we can go ahead */
  237.                         if (StartupItem == NIL)
  238.                             {
  239.                                 /* if we haven't opened an untitled document, and the thing */
  240.                                 /* returned True, except it returned a NIL item, then we */
  241.                                 /* can open an untitled document */
  242.                             }
  243.                          else
  244.                             {
  245.                                 /* if it isn't NIL, then open the actual thing */
  246.                                 /* this swallows the startup item record so we don't have */
  247.                                 /* do dispose of it */
  248.                                 OpenDisplayWindow(StartupItem);
  249.                             }
  250.                     }
  251.                 /* handle an event */
  252.                 TheEvent = GetAnEvent(&XLoc,&YLoc,&Modifiers,&Window,&MenuItem,&KeyPress);
  253.                 switch (TheEvent)
  254.                     {
  255.                         case eMouseUp:
  256.                             break;
  257.                         case eMenuStarting:
  258.                             EnableMenuItem(mAboutThisProgram);
  259.                             EnableMenuItem(mOpenFile);
  260.                             EnableMenuItem(mQuit);
  261.                             DispatchMenuStarting(Window);
  262.                             break;
  263.                         case eMenuCommand:
  264.                             if (MenuItem == mAboutThisProgram)
  265.                                 {
  266.                                     AlertInfo("NewsView written by Thomas R. Lawrence "
  267.                                         "(tomlaw@world.std.com), 1994, to be an exceedingly simple "
  268.                                         "offline news reader.",NIL);
  269.                                 }
  270.                             else if (MenuItem == mOpenFile)
  271.                                 {
  272.                                     FileSpec*                    Where;
  273.  
  274.                                     Where = GetFileAny();
  275.                                     if (Where != NIL)
  276.                                         {
  277.                                             OpenDisplayWindow(Where);
  278.                                         }
  279.                                 }
  280.                             else if (MenuItem == mQuit)
  281.                                 {
  282.                                     SetQuitPending();
  283.                                 }
  284.                             else
  285.                                 {
  286.                                     DispatchProcessMenuCommand(Window,MenuItem);
  287.                                 }
  288.                             break;
  289.                         case eKeyPressed:
  290.                             DispatchDoKeyDown(Window,KeyPress,Modifiers);
  291.                             break;
  292.                         case eMouseDown:
  293.                             DispatchDoMouseDown(Window,XLoc,YLoc,Modifiers);
  294.                             break;
  295.                         case eWindowClosing:
  296.                             DispatchCloseWindow(Window);
  297.                             break;
  298.                         case eWindowResized:
  299.                             DispatchWindowJustResized(Window);
  300.                             break;
  301.                         case eActiveWindowChanged:
  302.                             DispatchActiveWindowJustChanged(Window);
  303.                             break;
  304.                         case eNoEvent:
  305.                         case eCheckCursor:
  306.                             DispatchDoIdle(Window,TheEvent == eCheckCursor,XLoc,YLoc,Modifiers);
  307.                             break;
  308.                         default:
  309.                             EXECUTE(PRERR(AllowResume,"Unimplemented event received"));
  310.                             break;
  311.                     }
  312.                 if (CheckQuitPending())
  313.                     {
  314.                         /* ask "are you sure" here; */
  315.                         if (DoCloseAllQuitPending())
  316.                             {
  317.                                 ContinueFlag = False;
  318.                             }
  319.                          else
  320.                             {
  321.                                 AbortQuitInProgress();
  322.                             }
  323.                     }
  324.             }
  325.         ClearStartupDocuments();
  326.  
  327.         /* application cleanup */
  328.      AfterMenuFailed:
  329.         KillMenuAndDeleteItems(mmEditMenu);
  330.      MakeEditMenuFailed:
  331.         KillMenuAndDeleteItems(mmFileMenu);
  332.      MakeFileMenuFailed:
  333.         KillMenuAndDeleteItems(mmAppleMenu);
  334.      MakeAppleMenuFailed:
  335.         KillWindowStuff();
  336.  
  337.         /* level 1 cleanup */
  338.      InitWindowStuffFailed:
  339.         ShutdownWindowDispatcher();
  340.      InitWindowDispatcherFailed:
  341.         ShutdownGrowIcon();
  342.      InitGrowFailedPoint:
  343.         ShutdownAlertSubsystem();
  344.  
  345.      InitAlertFailedPoint:
  346.         /* level 0 cleanup */
  347.         ShutdownScreen();
  348.  
  349.      InitScreenFailedPoint:
  350.         APRINT(("-main"));
  351.         return 0;
  352.     }
  353.